package edu.cebanc.spring.biblioteca.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.cebanc.spring.biblioteca.bean.AutorBean;
import edu.cebanc.spring.biblioteca.bean.EditorialBean;
import edu.cebanc.spring.biblioteca.bean.GeneroBean;
import edu.cebanc.spring.biblioteca.bean.IdiomaBean;
import edu.cebanc.spring.biblioteca.service.DatosGeneralService;

@Controller
@RequestMapping(value="/datosGeneral")
public class DatosGeneralController {

	@Autowired
	DatosGeneralService datosGeneralService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String getMainView(Model model){
		
		model.addAttribute("autores", datosGeneralService.listadoAutores());
		model.addAttribute("generos", datosGeneralService.listadoGeneros());
		model.addAttribute("idiomas", datosGeneralService.listadoIdiomas());
		model.addAttribute("editoriales", datosGeneralService.listadoEditoriales());
		
		return "datosGeneralMain";
	}
	
	@RequestMapping(value="/autorForm", method=RequestMethod.GET)
	public String getAutorFormView(Model model){
		
		AutorBean ab = new AutorBean();
		model.addAttribute("autor", ab);
		
		return "autorForm";
	}
	
	@RequestMapping(value="/autorInfo/{id}", method=RequestMethod.GET)
	public String getAutorInfoView(@PathVariable String id, Model model){
		
		model.addAttribute("autor", datosGeneralService.cargarAutor(id));
		
		return "autorInfo";
	}
	
	@RequestMapping(value="/dataAutor", method=RequestMethod.POST)
	public String dataAutor(@Valid AutorBean autor, BindingResult result, Model model){
		if(result.hasErrors()){
			model.addAttribute("autor", autor);
			return "autorForm";
		}
		
		datosGeneralService.guardarAutor(autor);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="editAutor/{id}", method=RequestMethod.GET)
	public String editAutor(@PathVariable String id, Model model){
		
		model.addAttribute("autor", datosGeneralService.cargarAutor(id));
		
		return "autorForm";
	}
	
	@RequestMapping(value="deleteAutor/{id}", method=RequestMethod.GET)
	public String deleteAutor(@PathVariable String id){
		
		datosGeneralService.eliminarAutor(id);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="/generoForm", method=RequestMethod.GET)
	public String getGeneroFormView(Model model){
		
		GeneroBean gb = new GeneroBean();
		model.addAttribute("genero", gb);
		
		return "generoForm";
	}
	
	@RequestMapping(value="/generoInfo/{id}", method=RequestMethod.GET)
	public String getGeneroInfoView(@PathVariable String id, Model model){
		
		model.addAttribute("genero", datosGeneralService.cargarGenero(id));
		
		return "generoInfo";
	}
	
	@RequestMapping(value="/dataGenero", method=RequestMethod.POST)
	public String dataGenero(@Valid GeneroBean genero, BindingResult result, Model model){
		if(result.hasErrors()){
			model.addAttribute("genero", genero);
			return "generoForm";
		}
		
		datosGeneralService.guardarGenero(genero);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="editGenero/{id}", method=RequestMethod.GET)
	public String editGenero(@PathVariable String id, Model model){
		
		model.addAttribute("genero", datosGeneralService.cargarGenero(id));
		
		return "generoForm";
	}
	
	@RequestMapping(value="deleteGenero/{id}", method=RequestMethod.GET)
	public String deleteGenero(@PathVariable String id){
		
		datosGeneralService.eliminarGenero(id);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="/idiomaForm", method=RequestMethod.GET)
	public String getIdiomaFormView(Model model){
		
		IdiomaBean ib = new IdiomaBean();
		model.addAttribute("idioma", ib);
		
		return "idiomaForm";
	}
	
	@RequestMapping(value="/idiomaInfo/{id}", method=RequestMethod.GET)
	public String getIdiomaInfoView(@PathVariable String id, Model model){
		
		model.addAttribute("idioma", datosGeneralService.cargarIdioma(id));
		
		return "idiomaInfo";
	}
	
	@RequestMapping(value="/dataIdioma", method=RequestMethod.POST)
	public String dataIdioma(@Valid IdiomaBean idioma, BindingResult result, Model model){
		if(result.hasErrors()){
			model.addAttribute("idioma", idioma);
			return "idiomaForm";
		}
		
		datosGeneralService.guardarIdioma(idioma);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="editIdioma/{id}", method=RequestMethod.GET)
	public String editIdioma(@PathVariable String id, Model model){
		
		model.addAttribute("idioma", datosGeneralService.cargarIdioma(id));
		
		return "idiomaForm";
	}
	
	@RequestMapping(value="deleteIdioma/{id}", method=RequestMethod.GET)
	public String deleteIdioma(@PathVariable String id){
		
		datosGeneralService.eliminarIdioma(id);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="/editorialForm", method=RequestMethod.GET)
	public String getEditorialFormView(Model model){
		
		EditorialBean eb = new EditorialBean();
		model.addAttribute("editorial", eb);
		
		return "editorialForm";
	}
	
	@RequestMapping(value="/editorialInfo/{id}", method=RequestMethod.GET)
	public String getEditorialInfoView(@PathVariable String id, Model model){
		
		model.addAttribute("editorial", datosGeneralService.cargarEditorial(id));
		
		return "editorialInfo";
	}
	
	@RequestMapping(value="/dataEditorial", method=RequestMethod.POST)
	public String dataEditorial(@Valid EditorialBean editorial, BindingResult result, Model model){
		if(result.hasErrors()){
			model.addAttribute("editorial", editorial);
			return "editorialForm";
		}
		
		datosGeneralService.guardarEditorial(editorial);
		
		return "redirect:/datosGeneral";
	}
	
	@RequestMapping(value="editEditorial/{id}", method=RequestMethod.GET)
	public String editEditorial(@PathVariable String id, Model model){
		
		model.addAttribute("editorial", datosGeneralService.cargarEditorial(id));
		
		return "editorialForm";
	}
	
	@RequestMapping(value="deleteEditorial/{id}", method=RequestMethod.GET)
	public String deleteEditorial(@PathVariable String id){
		
		datosGeneralService.eliminarEditorial(id);
		
		return "redirect:/datosGeneral";
	}
}
